package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CaseDao;
import com.dao.CommentDao;
import com.pojo.CommentPojo;
import com.util.JsonUtil;
import com.util.Log;

@WebServlet(urlPatterns={"/admin/LoadCommentList"})
public class LoadCommentList extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String currentCaseId = request.getParameter("currentCaseId");
		String caseId = request.getParameter("caseId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String guestName = request.getParameter("guestName");
		String commentContent = request.getParameter("commentContent");
		String verifyStatus = request.getParameter("verifyStatus");
		
		// 当前页
		int intPage = Integer.parseInt((pageNum == null || pageNum == "0") ? "1"
				: pageNum);
		// 每页显示条数
		int number = Integer.parseInt((pageSize == null || pageSize == "0") ? "10"
				: pageSize);
		
		
		CommentDao commentDao = new CommentDao();
		CaseDao caseDao = new CaseDao();
		String sWhere = " where 1=1 ";
		
		if (currentCaseId != null && !"".equals(currentCaseId)) {
			if(Integer.parseInt(currentCaseId) > 0) {
				sWhere += " and caseId=" + Integer.parseInt(currentCaseId);
			}
		}
		if (caseId != null && !"".equals(caseId)) {
			if(Integer.parseInt(caseId) > 0) {
				sWhere += " and caseId=" + Integer.parseInt(caseId);
			}
		}
		if (guestName != null && !"".equals(guestName))
			sWhere += " and  guestName like '%" + guestName + "%'";
		if (commentContent != null && !"".equals(commentContent))
			sWhere += " and  content like '%" + commentContent + "%'";
		if (verifyStatus != null && !"".equals(verifyStatus))
			sWhere += " and  verifyResult = " + Integer.parseInt(verifyStatus);
		

		List<CommentPojo> list = commentDao.loadComment(intPage, number, sWhere);
		String[] allCaseNames = caseDao.loadCaseName();
		
		
		for(int i = 0; i < list.size(); i++) {
			int id = list.get(i).getCaseId();
			list.get(i).setCaseName(allCaseNames[id - 1]);
		}
		

		String sList = "{";
		sList += "\"total\":" + commentDao.tabCount + ", \"rows\":";
		sList += JsonUtil.ListToJSON(list);
		sList += "}";
		commentDao.closeAll();
		Log.write(sList);

		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		out.println(sList);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
